package com.education.service.task;

import com.education.common.utils.SpringBeanManager;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 任务管理器
 * @author 余一
 * @Description
 * @date 2021/6/13 11:54
 */
public class TaskManager {

    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    //创建map对监听器进行缓存
//    private final Map<String,TaskListener> taskListenerMap = new ConcurrentHashMap<>();

    public TaskManager(ThreadPoolTaskExecutor threadPoolTaskExecutor) {
        this.threadPoolTaskExecutor = threadPoolTaskExecutor;
    }

    public void pushTask(TaskParam taskParam) {
        TaskListener taskListener = SpringBeanManager.getBean(taskParam.getTaskListenerClass()); //taskListenerMap.get(beanName);
        if (taskListener != null) {
            threadPoolTaskExecutor.execute(() -> {
                taskListener.onMessage(taskParam);
            });
        }
    }
    public void pushTask(Runnable task) {
        threadPoolTaskExecutor.execute(task);
    }
}
